//
//  LXMineHeaderView.m
//  LvXi_newVersion
//
//  Created by 诸葛孔明 on 16/8/10.
//  Copyright © 2016年 SJT. All rights reserved.
//............................................................................................//
//......................................个人中心头部视图......................................//
//............................................................................................//

#import "LXMineHeaderView.h"
#import "LXMyGreenBeansViewController.h"

@interface LXMineHeaderView ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
//返回
@property(nonatomic,weak)UIButton *leftButton;
//设置
@property(nonatomic,weak)UIButton *rightButton;

@property(nonatomic,strong)LXUser * user;

@end

@implementation LXMineHeaderView

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        self.user = [LXUser shareUser];
        self.backgroundColor = LXMainColor;
        
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setBackgroundImage:[UIImage imageNamed:@"mine_back"] forState:UIControlStateNormal];
        leftButton.imageView.contentMode = UIViewContentModeScaleToFill;
        [leftButton addTarget:self action:@selector(leftButtonClick) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:leftButton];
        self.leftButton = leftButton;
        
        UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [rightButton setTitle:@"设置" forState:UIControlStateNormal];
        [rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [rightButton addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
        rightButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [self addSubview:rightButton];
        self.rightButton = rightButton;
        
        UIImageView *userImageView = [[UIImageView alloc] init];
        userImageView.userInteractionEnabled = YES;
        [userImageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeHeaderimage:)]];
        [self addSubview:userImageView];
        self.userImageView = userImageView;
        [userImageView sd_setImageWithURL:[NSURL URLWithString:self.user.headerImage] placeholderImage:[UIImage imageNamed:@"user_icon_default"]];
        
        UILabel *nameLabel = [[UILabel alloc] init];
        nameLabel.text = LoginState?User_nickname:@"暂未登录";
        nameLabel.textColor = [UIColor whiteColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:nameLabel];
        self.nameLabel = nameLabel;
        
        UILabel *IDLabel = [[UILabel alloc] init];
        IDLabel.text = @"ID:001";
        IDLabel.textColor = [UIColor whiteColor];
        IDLabel.textAlignment = NSTextAlignmentCenter;
        IDLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:IDLabel];
        self.IDLabel = IDLabel;
        
        UIButton *greenBeansButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [greenBeansButton setBackgroundImage:[UIImage imageNamed:@"greenBtnBag"] forState:UIControlStateNormal];
        [greenBeansButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        [greenBeansButton setTitleEdgeInsets:UIEdgeInsetsMake(0, 5, 0, 0)];
        [greenBeansButton addTarget:self action:@selector(greenBeanBtnClick) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:greenBeansButton];
        self.greenBeansButton = greenBeansButton;
    }
    return self;
}

#pragma mark - 布局子控件
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //返回按钮
    self.leftButton.left = 20;
    self.leftButton.top = 31;
    self.leftButton.width = MineRatio(9);
    self.leftButton.height = MineRatio(16);
    
    //设置按钮
    self.rightButton.y = 30;
    self.rightButton.width = MineRatio(50);
    self.rightButton.height = MineRatio(24);
    self.rightButton.x = ScreenWidth - self.rightButton.width - 12;
    
    //用户头像
    self.userImageView.width = MineRatio(110);
    self.userImageView.height = MineRatio(110);
    self.userImageView.x = self.width*0.5 - self.userImageView.width * 0.5;
    self.userImageView.y = self.height*0.5-self.userImageView.height * 0.5;
    [self.userImageView addRadius:self.userImageView.width*0.5];
    
    //名字
    self.nameLabel.height = MineRatio(15);
    self.nameLabel.width = MineRatio(200);
    self.nameLabel.x = self.width*0.5 - self.nameLabel.width * 0.5;
    self.nameLabel.top = self.userImageView.botton + 10;

    
    //ID
    self.IDLabel.height = self.nameLabel.height;
    self.IDLabel.width = self.nameLabel.width;
    self.IDLabel.x = self.width*0.5 - self.IDLabel.width * 0.5;
    self.IDLabel.y = self.nameLabel.botton + 5;

    
    //绿豆
    self.greenBeansButton.width = 65;
    self.greenBeansButton.height = 25;
    self.greenBeansButton.x = self.width - self.greenBeansButton.width;
    self.greenBeansButton.y = (self.height - self.greenBeansButton.height) * 0.5;
}

#pragma mark - 返回按钮点击
- (void)leftButtonClick
{
    if (self.leftHandle) {
        self.leftHandle();
    }
}

#pragma mark - 设置按钮点击
- (void)rightButtonClick
{
    if (self.rightHandle) {
        self.rightHandle();
    }
}

#pragma mark - 绿豆按钮点击
- (void)greenBeanBtnClick
{
    LXMyGreenBeansViewController *greenBeansVc = [[LXMyGreenBeansViewController alloc] init];
    [[self getCurrentVC].navigationController pushViewController:greenBeansVc animated:YES];

}


#pragma mark - 头像点击事件
-(void)changeHeaderimage:(UITapGestureRecognizer*)gesture{
    if(LoginState){
        [self alertWithTitle:@"更换头像？" mesege:@"" buttons:@[@"从相册选取",@"现在拍一个",@"取消",] handle:^(NSInteger buttonIndex) {
            switch (buttonIndex) {
                case 2:break;
                case 0:{
                    UIImagePickerController *imagepicker = [[UIImagePickerController alloc] init];
                    imagepicker.delegate = self;
                    imagepicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                    imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                    imagepicker.allowsEditing = YES;
                    [[self getCurrentVC] presentViewController:imagepicker animated:YES completion:nil];
                }break;
                case 1:{
                    UIImagePickerController *imagepicker = [[UIImagePickerController alloc] init];
                    imagepicker.delegate = self;
                    imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                    imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
                    imagepicker.allowsEditing = YES;
                    [[self getCurrentVC] presentViewController:imagepicker animated:YES completion:nil];
                }break;
                default:
                    break;
            }
        }];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    [[LXDataManager manager] upLoadHeaderImage:image uid:self.user.user_id complete:^(BOOL success, id res) {
        if(success){
            LXUser * user = [LXUser shareUser];
            user.headerImage = res[@"data"][@"path"];
            self.userImageView.image = image;
        }
    }];
    [picker  dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - imagePicker 取消选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker  dismissViewControllerAnimated:YES completion:nil];
}
@end
